package com.dingli.service.impl;

import com.dingli.framework.entity.Member;
import com.dingli.framework.entity.News;
import com.dingli.framework.mapper.NewsMapper;
import com.dingli.framework.util.Response;
import com.dingli.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsService implements INewsService {

    @Autowired
    private NewsMapper newsMapper;



    @Override
    public Response<PageInfo<News>> NEWS_LIST(String search, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<News> info = new PageInfo<>(newsMapper.NEWS_LIST(search));
        return new Response<>(200, "success", info);
    }


    @Override
    public Response<Long> NEWS_insert(News news) {
        int count = newsMapper.NEWS_insert(news);
        if (count > 0) {
            return new Response(200, "success", news.getNid());
        } else {
            return new Response<>(500, "failed");
        }
    }

    @Override
    public Response<News> NEWS_selectById(Long nID) {
        return new Response(200, "success", newsMapper.NEWS_selectById(nID));
    }

    @Override
    public Response<Object> NEWS_updateById(News news) {
        int count = newsMapper.NEWS_updateById(news);
        if (count > 0) {
            return new Response<>(200, "success");
        } else {
            return new Response<>(500, "failed");
        }
    }

    @Override
    public Response<Object> NEWS_deleteById(Long nID) {
        int count = newsMapper.NEWS_deleteById(nID);
        if (count > 0) {
            return new Response<>(200, "success");
        } else {
            return new Response<>(500, "failed");
        }
    }
}
